package org.genuflex.ui
{
	import org.genuflex.components.GenuflexEditWindow;
	import org.genuflex.control.GenuflexController;
	import flash.utils.getDefinitionByName;
	import mx.events.CloseEvent;
	import flash.display.Sprite;
	import mx.controls.Alert;
	import org.genuflex.control.GenuflexCairngormEvent;
	import org.genuflex.vo.GenuflexValueObject;
	import flash.display.DisplayObject;
	import org.genuflex.utils.GenuflexCairngormHelper;
	import org.genuflex.model.GenuflexModelLocator;
	import mx.managers.PopUpManager;
	import org.genuflex.components.GenuflexSelectWindow;
	
	public class GenuflexManager
	{
		
		public var model : GenuflexModelLocator;
		public var modelCollectionName : String;
		public var title : String; 
		public var titlePlural : String;
		public var className : String;
		public var eventArgumentsObjectName : String;
		public var voClass : Class;
		public var editWindowClass : Class;
		public var voLabelFunction : Function;
		public var findByNameCommand : String;
		
		/* Load all of this type of object from the server */				
		public function handleRefreshAll(forceReset : Boolean = false) : void {
	
			//Should we wipe the model locator's array collection of these objects
			if (forceReset) {
				model[modelCollectionName] = null;
			}
	
			GenuflexCairngormHelper.dispatchEvent(GenuflexController.getFindAllCommandName(className));
		}
		
		/* Add a new object */				
		public function handleAdd(displayObject : Object, objectToAdd : GenuflexValueObject = null, modal : Boolean = true) : void {
			var editWindow : GenuflexEditWindow = GenuflexEditWindow(PopUpManager.createPopUp(displayObject as DisplayObject, editWindowClass, modal));
	
			if (objectToAdd == null) {
				objectToAdd = new voClass();
			}

			editWindow.setCurrentVO(objectToAdd);
			editWindow.saveFunction = save;
		}

		/* Save the object */
		public function save (objectToSave : Object) : void {

			// Create, configure and dispatch a save event
			var genuflexEvent : GenuflexCairngormEvent = new GenuflexCairngormEvent(GenuflexController.getSaveCommandName(className));
			genuflexEvent.eventArguments[eventArgumentsObjectName] = objectToSave;
			GenuflexCairngormHelper.dispatchGenuflexCairngormEvent(genuflexEvent);

		}				

		/* Edit the selected object */
		public function handleEdit(displayObject : Object, objectToEdit : Object, modal : Boolean = true) : void {
			if (objectToEdit != null) {
		
				//Create the edit window
				var editWindow : GenuflexEditWindow = GenuflexEditWindow(PopUpManager.createPopUp(displayObject as DisplayObject, editWindowClass, modal));
				editWindow.saveFunction = save;
		
				// Load the object from the server again
				var genuflexEvent : GenuflexCairngormEvent = new GenuflexCairngormEvent(GenuflexController.getFindByIdCommandName(className));
				genuflexEvent.eventArguments["id"] = objectToEdit.id;
				genuflexEvent.eventArguments["additionalResultFunction"] = editWindow.setCurrentVO;
				GenuflexCairngormHelper.dispatchGenuflexCairngormEvent(genuflexEvent);
		

			}
		}

		/* Be sure that the user wants this object destroyed */				
		public function handleDestroy(sprite : Sprite, objectToDestroy : Object) : void {
			if (objectToDestroy != null) {
				var alert : Alert = Alert.show("Are you sure you want to delete this "+title+"?", "Delete "+title, 3, sprite, destroy, null, Alert.YES);
				alert.data = objectToDestroy;
			}					
		}

		/* Destroy the selected object */
		private function destroy(event : CloseEvent) : void {

			if (event.detail==Alert.YES) {

				// Grab the objectToDestroy off of the Alert (currentTarget)
				if (event.currentTarget != null && event.currentTarget.data != null) {
				  // Create, configure and dispatch a destroy event
				  var genuflexEvent : GenuflexCairngormEvent = new GenuflexCairngormEvent(GenuflexController.getDestroyCommandName(className));
				  genuflexEvent.eventArguments[eventArgumentsObjectName] = event.currentTarget.data;
				  GenuflexCairngormHelper.dispatchGenuflexCairngormEvent(genuflexEvent);
			  }
			}
		}
		
		/* Pop up the selection window */
		public function handleSelect(displayObject : DisplayObject, currentVO : Object, foreignKeyField : String, propertyName : String, selectManyMode : Boolean = false, saveFunction : Function = null) : void {
	    	var selectionWindow : GenuflexSelectWindow = GenuflexSelectWindow(PopUpManager.createPopUp(displayObject, GenuflexSelectWindow, true));

			selectionWindow.currentObject = currentVO;				
			selectionWindow.foreignKeyField = foreignKeyField;
			selectionWindow.propertyName = propertyName;
			selectionWindow.selectManyMode = selectManyMode;
			selectionWindow.saveFunction = saveFunction;
			selectionWindow.itemLabel = title;
			selectionWindow.manager = this;
			selectionWindow.modelCollectionName = modelCollectionName;
			selectionWindow.itemsLabel = titlePlural;
			selectionWindow.findByNameCommand = findByNameCommand;
			selectionWindow.voLabelFunction = voLabelFunction;
	    }		
	}
}